package com.zhong.gang.pda.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.applib.controller.ImgController;
import com.zhong.gang.applib.ui.preview.PreviewAct;
import com.zhong.gang.applib.utils.DiverItemDecoration;
import com.zhong.gang.applib.ui.adapter.ImgAdapter;
import com.zhong.gang.applib.utils.PictureUtils;
import com.zhong.gang.applib.utils.TimeUtils;
import com.zhong.gang.pda.R;
import com.zhong.gang.pda.databinding.ItemHuoFeetBinding;
import com.zhong.gang.pda.ui.action.HuoBean;

import java.util.ArrayList;
import java.util.List;

public class HuoFeetView extends FrameLayout implements BaseQuickAdapter.OnItemClickListener {
    private ItemHuoFeetBinding binding;
    private ImgAdapter firstAdapter;
    private ImgAdapter secondAdapter;
    public HuoFeetView(Context context) {
        super(context);
        addView();
    }

    public HuoFeetView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addView();
    }

    public HuoFeetView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addView();
    }

    private void addView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_huo_feet,null);
        binding = DataBindingUtil.bind(view);
        addView(view);
        binding.firstRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
        binding.secondRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
        binding.firstRecyclerView.setAdapter(firstAdapter = new ImgAdapter(false));
        binding.secondRecyclerView.setAdapter(secondAdapter = new ImgAdapter(false));
        binding.firstRecyclerView.setNestedScrollingEnabled(false);
        binding.secondRecyclerView.setNestedScrollingEnabled(false);
        firstAdapter.setOnItemClickListener(this);
        secondAdapter.setOnItemClickListener(this);
        DiverItemDecoration.addItemDecoration(binding.firstRecyclerView,getContext(),R.color.transparent,8,DiverItemDecoration.HORIZONTAL);
        DiverItemDecoration.addItemDecoration(binding.secondRecyclerView,getContext(),R.color.transparent,8,DiverItemDecoration.HORIZONTAL);
    }

    public ImgAdapter getFirstAdapter() {
        if (firstAdapter == null)
            firstAdapter = new ImgAdapter(false);
        return firstAdapter;
    }


    public ImgAdapter getSecondAdapter() {
        if (secondAdapter == null)
            secondAdapter = new ImgAdapter(false);
        return secondAdapter;
    }
    
    public void setData(HuoBean bean){

        if (!TextUtils.isEmpty(bean.getFirstTime())){
            binding.firstTimeLayout.setVisibility(VISIBLE);
            binding.firstWeightLayout.setVisibility(VISIBLE);
            binding.firstRecyclerViewLayout.setVisibility(VISIBLE);
            binding.firstTime.setText(TimeUtils.getTime(bean.getFirstTime()));
            binding.firstWeight.setText(bean.getFirstWeight()+"吨");
            if (!TextUtils.isEmpty(bean.getFirstPhotoHead().trim()))
                getFirstAdapter().addData(bean.getFirstPhotoHead().trim());
            if (!TextUtils.isEmpty(bean.getFirstPhotoTail().trim()))
                getFirstAdapter().addData(bean.getFirstPhotoTail().trim());
            if (!TextUtils.isEmpty(bean.getFirstPhotoTop().trim()))
                getFirstAdapter().addData(bean.getFirstPhotoTop().trim());
        }else {
            binding.firstTimeLayout.setVisibility(GONE);
            binding.firstWeightLayout.setVisibility(GONE);
            binding.firstRecyclerViewLayout.setVisibility(GONE);
        }

        if (!TextUtils.isEmpty(bean.getSecondTime())){
            binding.secondTimeLayout.setVisibility(VISIBLE);
            binding.secondWeightLayout.setVisibility(VISIBLE);
            binding.secondRecyclerViewLayout.setVisibility(VISIBLE);
            binding.secondTime.setText(TimeUtils.getTime(bean.getSecondTime()));
            binding.secondWeight.setText(bean.getSecondWeight()+"吨");
            if (!TextUtils.isEmpty(bean.getSecondPhotoHead().trim()))
                getSecondAdapter().addData(bean.getSecondPhotoHead().trim());
            if (!TextUtils.isEmpty(bean.getSecondPhotoTail().trim()))
                getSecondAdapter().addData(bean.getSecondPhotoTail().trim());
            if (!TextUtils.isEmpty(bean.getSecondPhotoTop()))
                getSecondAdapter().addData(bean.getSecondPhotoTop().trim());
        }else {
            binding.secondTimeLayout.setVisibility(GONE);
            binding.secondWeightLayout.setVisibility(GONE);
            binding.secondRecyclerViewLayout.setVisibility(GONE);
        }

        if (TextUtils.isEmpty(bean.getFirstTime()) && TextUtils.isEmpty(bean.getSecondTime())){
            binding.titleLayout.setVisibility(GONE);
        }else {
            binding.titleLayout.setVisibility(VISIBLE);
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<String> data = adapter.getData();
        PreviewAct.openAct(getContext(), (ArrayList<String>) data,position);
    }
}
